package bookStore.tool;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

public class UploadTool {
	public static String uploadFile(String path,String file,HttpServletRequest request) {
		File filePath = new File(path);
		if(!filePath.exists()) {
			filePath.mkdir();
		}
		try {
			Part part = request.getPart(file);
			String header = part.getHeader("content-disposition");
	            // 获取文件名
	        String fileName = header.split(";")[2].split("=")[1].replaceAll("\"", "");
	            // 执行写入操作 --- 上传到指定的目录
	        part.write(filePath+File.separator+fileName);
	        return fileName;
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
